//
//  GJRichButton.m
//  YDGJ
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "GJRichButton.h"
#import "FD_Method.h"

@interface GJRichButton ()

@property(nonatomic,copy) void(^actionBlock)(UIButton *);
@property(nonatomic,assign)CGFloat spacing;
@property(nonatomic,assign)GJButonSubViewLayout layout;
@property(nonatomic,assign)CGFloat markNumber;
@property(nonatomic,assign)BOOL btnNone;

@end

@implementation GJRichButton

+(instancetype)richBtnWithFrmae:(CGRect)frame titleBtn:(NSString *)title titleFontNumber:(CGFloat)fontSize normalImageName:(NSString *)norName selectedImageName:(NSString *)selecName viewSpacing:(CGFloat)spacing subViewLayout:(GJButonSubViewLayout)layout handClickBlock:(void(^)(UIButton *btn))clickblcok
{
    return [[self alloc]initWithFrame:frame titleBtn:title titleFontNumber:fontSize normalImageName:norName selectedImageName:selecName viewSpacing:spacing subViewLayout:layout handClickBlock:clickblcok];
}

-(instancetype)initWithFrame:(CGRect)frame titleBtn:(NSString *)title titleFontNumber:(CGFloat)fontSize normalImageName:(NSString *)norName selectedImageName:(NSString *)selecName viewSpacing:(CGFloat)spacing subViewLayout:(GJButonSubViewLayout)layout handClickBlock:(void(^)(UIButton *))clickblcok
{
    if (self = [super initWithFrame:frame])
    {
        self.actionBlock = clickblcok;
        self.spacing = spacing;
        self.layout = layout;
        if (GJButonLabelNone == layout) self.btnNone = YES;
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:Color_BlackFont forState:UIControlStateNormal];
        self.titleLabel.font = FontCustomSize(fontSize);
        [self setImage:[UIImage imageNamed:norName] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:selecName] forState:UIControlStateSelected];
        [self addTarget:self action:@selector(actionBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    if (self.btnNone) return;
    CGFloat maxSpacing;
    if (self.layout == GJButonLabelLeft || GJButonLabelRight == self.layout)
    {
        //固定Label宽度标记位,默认ImageView固定
        if (!self.markNumber) self.markNumber = self.titleLabel.width;
        
        maxSpacing = self.width - self.markNumber - self.imageView.width;
        if (self.spacing > maxSpacing) {
            self.spacing = maxSpacing - 20;
        }
        if (self.layout == GJButonLabelLeft)
        {
            self.titleLabel.x = (self.width - (self.markNumber+ self.imageView.width + self.spacing))*0.5;
            self.imageView.x = self.titleLabel.frame.origin.x+self.markNumber+self.spacing;
        }else if (GJButonLabelRight == self.layout){
            self.imageView.x = (self.width - (self.markNumber + self.imageView.width + self.spacing))*0.5;
            self.titleLabel.x = CGRectGetMaxX(self.imageView.frame)+self.spacing;
        }
        self.titleLabel.y = (self.height - self.titleLabel.height)*0.5;
        self.imageView.y = (self.height - self.imageView.height)*0.5;
    }
    
    else if (self.layout == GJButonLabelTop || GJButonLabelBottm == self.layout)
    {
        self.titleLabel.width = self.width;
        //固定Label高度标记位
        if (!self.markNumber) self.markNumber = self.titleLabel.height;
        maxSpacing = self.height - self.markNumber - self.imageView.height;
        if (self.spacing > maxSpacing) {
            self.spacing = maxSpacing - 10;
        }
        if (self.layout == GJButonLabelTop){
            self.titleLabel.y = (self.height - (self.markNumber+ self.imageView.height + self.spacing))*0.5;
            self.imageView.y = self.titleLabel.frame.origin.y+self.markNumber+self.spacing;
        }
        else if (GJButonLabelBottm == self.layout){
            self.imageView.y =(self.height - (self.markNumber+ self.imageView.height + self.spacing))*0.5;
            self.titleLabel.y = CGRectGetMaxY(self.imageView.frame)+self.spacing;
        }
        
        self.titleLabel.x = (self.width - self.titleLabel.width)*0.5;
        self.imageView.x = (self.width - self.imageView.width)*0.5;
    }
}


-(void)actionBtnClicked:(UIButton *)sender
{
    if (self.actionBlock) self.actionBlock(sender);
}

@end
